from django.shortcuts import render, HttpResponse, redirect
from django.core.paginator import Paginator

from .models import LeaveType
from .models import Leave
from .models import User


# Create your views here.
def index(request):
    print(request.session.get('role'))
    print(request.session.get('uname'))

    # 登录才能访问
    if not request.session.get('role'):
        return HttpResponse('这是首页')
    else:
        return HttpResponse('请先登录')

def add_leaver(request):
    if request.session.get('role') != 1:
        response = HttpResponse('没有权限,拒绝访问', status=403, charset='utf-8')
        response['content_type'] = 'text/html'
        return response

    if request.method == "GET":
        types = LeaveType.objects.all()
        return render(request, 'add_leaver.html', {"types": types})
    else:

        stu_name = request.POST.get('stu_name', '')
        context = request.POST.get('context', '')
        createTime = request.POST.get('createTime', '')
        tid = request.POST.get('tid', '')
        # 最好把str 转成int
        tid = int(tid)
        Leave.objects.create(
            stu_name=stu_name,
            context=context,
            createTime=createTime,
            tid_id=tid
        )
        return HttpResponse('插入成功')


def show_leaver(request):
    if request.session.get('role') != 2:
        response = HttpResponse('没有权限,拒绝访问', status=403, charset='utf-8')
        response['content_type'] = 'text/html'
        return response
    # http://127.0.0.1:8000/leave/show_leaver/?page=2&status=1
    leave_types = LeaveType.objects.all()
    status = request.GET.get("status", '-1')
    # 如果 status==-1 查询所有
    # 如果status 没有  查询所有
    # 其他情况   查询类型
    if status == '-1':
        leaves = Leave.objects.all()
    else:
        leaves = Leave.objects.filter(tid_id=int(status)).all()
    status = int(status)
    page = request.GET.get('page', 1)
    pageint = int(page)
    pagesize = 2  # 每页展示几条数据
    paginator = Paginator(leaves, pagesize)
    pagex = paginator.page(pageint)  # 获取第 x 页的数据对象
    leaves = pagex.object_list  # 是一个queryset对象

    return render(request, 'leaves.html',
                  {"leaves": leaves, "leave_types": leave_types, 'page': pageint, 'status': status})


def update_leaver(request):
    id = request.GET.get('id')
    status = request.GET.get('status')
    print(id)
    leave = Leave.objects.get(id=int(id))
    print(leave.stu_name)
    print(leave.createTime)
    leave.status = int(status)
    leave.save()
    return redirect("leave:show_leaver")


from hashlib import md5


def register(request):
    if request.method == "GET":
        return render(request, 'register.html')
    else:
        uname = request.POST.get('uname', '')
        upwd = request.POST.get('upwd', '')
        upwd2 = request.POST.get('upwd2', '')
        role = request.POST.get('role', '')

        if upwd == upwd2:  # 存数据, 返回登录界面
            try:
                User.objects.get(uname=uname)
                return render(request, 'register.html', {"error": '用户名重复,请换一个'})
            except:  # 没有 可以用这个用户名 保存
                upwd = md5(upwd.encode(encoding='UTF-8')).hexdigest()  # 将明文形式改成密文形式
                User.objects.create(
                    uname=uname,
                    upwd=upwd,
                    role=role
                )
                return HttpResponse("注册成功")

        else:  # 两次密码不一致
            return render(request, 'register.html', {"error": '两次密码不一样'})


from django.db.models import Q


def loginx(request):
    if request.method == "GET":
        return render(request, 'login.html')
    else:
        uname = request.POST.get('uname')
        upwd = request.POST.get('upwd')
        upwd = md5(upwd.encode(encoding='UTF-8')).hexdigest()

        user = User.objects.filter(Q(uname=uname) & Q(upwd=upwd)).last()
        if user:
            # 这两行代码
            request.session['uname'] = user.uname
            request.session['role'] = user.role
            if user.role == 1:  # 学生
                return redirect('leave:add_leaver')
            else:
                return redirect('leave:show_leaver')

        else:
            return redirect('leave:loginx')
